package com.wlz.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 *  日志切面
 * @author wlz
 * @date 2022-05-28  9:26 下午
 */
@Aspect
@Order
@Component
public class MyLogAspect {

	@Pointcut("execution(* com.wlz.aop.MyCalculate.*(..))")
	public void pointCut() {

	}


	@Before(value = "pointCut()")
	public void methodBefore(JoinPoint joinPoint) throws Throwable {
		String methodName = joinPoint.getSignature().getName();
		System.out.println("执行目标方法【"+methodName+"】的<前置通知>,入参"+ Arrays.asList(joinPoint.getArgs()));
	}

	@After(value = "pointCut()")
	public void methodAfter(JoinPoint joinPoint) {
		String methodName = joinPoint.getSignature().getName();
		System.out.println("执行目标方法【"+methodName+"】的<后置通知>,入参"+Arrays.asList(joinPoint.getArgs()));
	}

	@AfterReturning(value = "pointCut()",returning = "result")
	public void methodReturning(JoinPoint joinPoint, Object result) {
		String methodName = joinPoint.getSignature().getName();
		System.out.println("执行目标方法【"+methodName+"】的<返回通知>,入参"+Arrays.asList(joinPoint.getArgs()));
	}

	@AfterThrowing(value = "pointCut()")
	public void methodAfterThrowing(JoinPoint joinPoint) {
		String methodName = joinPoint.getSignature().getName();
		System.out.println("执行目标方法【"+methodName+"】的<异常通知>,入参"+Arrays.asList(joinPoint.getArgs()));
	}

//	@Around(value = "pointCut()")
//	public Object around(ProceedingJoinPoint point) throws Throwable {
//		String methodName = point.getSignature().getName();
//		System.out.println("执行目标方法【"+methodName+"】的<环绕通知>,入参"+Arrays.asList(point.getArgs()));
//		Object[] args = point.getArgs();
//		for(int i=0;i<args.length;i++){
//			System.out.println("====args:"+args[i]);
//			if(args[i].getClass().equals(String.class)){
//				args[i] += "xxxx";
//			}
//		}
//
//		// before
//		Object result = point.proceed(args);
//
//		// 处理逻辑
//		System.out.println("执行目标方法【"+methodName+"】的<环绕通知>,结果"+ result.toString());
//		return result;
//	}


}
